var combineLevels = [20, 45];

function getCombineLevel(userLevel){
    for( var i=0,max=combineLevels.length; i<max; i++ ) {
        if( combineLevels[i] > userLevel ) {
            break;
        }
    }

    if( i<=0 ) {
        return 0;
    }

    return combineLevels[i-1];
}

exports.get_stone = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name || user.combine.stone || user.combine.occupying ) {
            resp.code = 1; resp.desc = 'invalid op'; break;
        } 
        
        if( user.combine.got >= gConfGlobal.CombineGotStoneDailyLimit ) {
            resp.code = 1; resp.desc = 'got'; break;
        }

    } while(false);

    if( 1 == resp.code ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            if( resp.data.stone ) {
                user.combine.stone = resp.data.stone;  
                user.combine.got += 1;
                player.markDirty('combine.stone');
                player.markDirty('combine.got');
            }
        }

        onHandled();
    });
}

exports.get = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'invalid op'; break;
        } 
        
        var page = parseInt(req.args.page);
        if( isNaN(page) || page < 1 || page > 10 ) {
            resp.code = 1; resp.desc = 'invalid page'; break;
        }

    } while(false);

    if( 1 == resp.code ) {
        onHandled();
        return;
    }
    
    req.args.level = user.status.level;
    req.args.occupying = user.combine.occupying;
    req.args.league_name = user.league.name;

    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            var combine = user.combine;
            if( resp.data.awards ) {
                resp.data.awards = player.addAwards(resp.data.awards);
                combine.stone = 0; 
                combine.combined += 1;
                combine.occupying = 0;
                player.markDirty('combine');
            }else if( resp.data.robbed ){
                combine.occupying = 0;
                if( resp.data.lost ) {
                    combine.stone = 0; 
                    player.markDirty('combine.stone');
                }
                
                player.markDirty('combine.occupying');
            }
        }
        onHandled();
    });
}

exports.rob = function(player, req, resp, onHandled) {
    var user = player.user;
    var atkFightForce = [0,0,0];
    do {
        if( !user.league.name || user.combine.stone || user.combine.occupying ) {
            resp.code = 1; resp.desc = 'invalid op'; break;
        } 

        if( user.combine.combined >= gConfGlobal.CombineDailyLimit ) {
            resp.code = 1; resp.desc = 'no times'; break;
        }

        var id = parseInt(req.args.id);
        if( isNaN(id) || id < 1 || id > 40 || req.args.rids.constructor != Array
            || req.args.rids.length != 3 || !(req.args.side == 'left' || req.args.side == 'right') ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var atkRoleArr = req.args.rids;
        var isRidOk = true;
        for( var i = 0, len = atkRoleArr.length; i < len; i++ ){
            var rid = atkRoleArr[i];

            if( !rid ){
                isRidOk = false;
                break;
            }

            var role = user.role[rid];

            if( !role ) {
                isRidOk = false;
                break;
            } 

            player.calculateRoleAttr(role);
            atkFightForce[i] = role.fight_force;
        }

        if( !isRidOk ){
            resp.code = 1; resp.desc = "invalid roles"; break;
        }

    } while(false);

    if( 1 == resp.code ) {
        onHandled();
        return;
    }

    req.args.fight_forces = atkFightForce;
    req.args.level = user.status.level;
    req.args.league_name = user.league.name;

    requestWorld(req, resp, function() {
        if( resp.code == 0  && !resp.data.update ) {
            if( resp.data.stone ) {
                user.combine.stone = resp.data.stone; 
                player.markDirty('combine.stone');
            }
        }

        onHandled();
    });
}

exports.join = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name || !user.combine.stone || user.combine.occupying ) {
            resp.code = 1; resp.desc = 'invalid op'; break;
        } 

        if( Object.keys(user.role).length < 3 ) {
            resp.code = 1;  resp.desc = 'at least 3 roles'; break;
        }   

        var id = parseInt(req.args.id);
        if( isNaN(id) || id < 1 || id > 40 ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        if( user.combine.combined >= gConfGlobal.CombineDailyLimit ) {
            resp.code = 1; resp.desc = 'no times'; break;
        }

    } while(false);

    if( 1 == resp.code ) {
        onHandled();
        return;
    }

    req.args.level = user.status.level;
    req.args.stone = user.combine.stone;
    req.args.league_name = user.league.name;
    requestWorld(req, resp, function() {
        if( resp.code == 0 && !resp.data.update) {
            user.combine.occupying = {
                'level' : resp.data.level || getCombineLevel(user.status.level),
                'id': parseInt(req.args.id),
            } 

            player.markDirty('combine.occupying');
        }

        onHandled();
    });
}

exports.leave = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name || !user.combine.stone || !user.combine.occupying ) {
            resp.code = 1; resp.desc = 'invalid op'; break;
        } 
        
    } while(false);

    if( 1 == resp.code ) {
        onHandled();
        return;
    }
    
    req.args.occupying = user.combine.occupying;
    req.args.stone = user.combine.stone;

    requestWorld(req, resp, function() {
        if( resp.code == 0 && !resp.data.update ) {
            if( 1 == resp.data.robbed ) {
                user.combine.stone = 0;      
                player.markDirty('combine.stone');
            }

            user.combine.occupying = 0; 
            player.markDirty('combine.occupying');
        }

        onHandled();
    });
}

exports.change_stone = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if (!user.league.name || !user.combine.stone) {
            resp.code = 1; resp.desc = 'invalid stone'; break;
        }

        if (user.combine.occupying) {
            resp.code = 1; resp.desc = 'occupying'; break;
        }

        var costGold = gConfGlobal.CombineCost;
        if (!player.costResource('gold', costGold)) {
            resp.code = 1; resp.desc = 'lack of gold'; break;
        }

        var combineConf = gConfCombine[user.combine.stone];
        user.combine.stone = combineConf.Tother;
        player.markDirty('combine.stone');

        resp.data.stone = user.combine.stone;
        resp.data.gold = -costGold;
        
    } while(false);

    onHandled && onHandled();
}
